package com.pos;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.pos.modules.bean.OrderBean;
import com.pos.set.Log;

import java.util.ArrayList;
import java.util.List;

public class OrderDatabaseHelper extends SQLiteOpenHelper {
    private SQLiteDatabase database;
    public static final String CREATE_ORDER = "create table ordertable ("
            + "id text, "
            + "payStatus text, "
            + "payMethod text, "
            + "payBillNumber text, "
            + "payDate text,"
            +"payAmount text, "
            +"cashAmount text, "
            +"cashChange text,"
            +"memberPayToken text, "
            +"secondPayNumber text, "
            +"merCode text, "
            +"merName text, "
            +"termCode text, "
            +"bankName text," +"bankCard text, "
            +"bankCardExpired text, "
            +"authorCode text, "
            +"tradeBatchCode text,"   +"orderCode text)";
    private Context mContext;
    public OrderDatabaseHelper(Context context) {
        super(context, "orderlite.db", null, 9);
        mContext = context;
        database = getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_ORDER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private void insert(String order_id, String payStatus, String payMethod, String payBillNumber, String payDate, String payAmount, String cashAmount, String cashChange,
                        String memberPayToken, String secondPayNumber, String merCode, String merName, String termCode, String bankName, String bankCard, String bankCardExpired, String authorCode,
                        String tradeBatchCode){
        ContentValues values=new ContentValues();
        values.put("id",order_id);
        values.put("payStatus",payStatus);
        values.put("payMethod",payMethod);
        values.put("payBillNumber",payBillNumber);
        values.put("payDate",payDate);
        values.put("payAmount",payAmount);
        values.put("cashAmount",cashAmount);
        values.put("cashChange",cashChange);
        values.put("memberPayToken",memberPayToken);
        values.put("secondPayNumber",secondPayNumber);
        values.put("merCode",merCode);
        values.put("merName",merName);
        values.put("termCode",termCode);
        values.put("bankName",bankName);
        values.put("bankCard",bankCard);
        values.put("bankCardExpired",bankCardExpired);

        values.put("authorCode",authorCode);
        values.put("tradeBatchCode",tradeBatchCode);
    }

    public void  deleid(String order_id){
        String sql="delete from ordertable where id='"+order_id+"'";
        database.execSQL(sql);

    }
    /**
     * 添加数据
     *
     * @param cv
     * @return
     */
    public boolean insertData(ContentValues cv) {
        return database.insert("ordertable", null, cv) > 0;
    }
    /**
     * 查询所有数据
     *
     * @return
     */
    public List<OrderBean> queryData() {

        List<OrderBean> carts = new ArrayList<OrderBean>();
        // 从数据库里查询数据
        Cursor cursor = database.query("ordertable", null, null,
                null, null, null, null);

        if (cursor != null) {
            // 取出数据
            while (cursor.moveToNext()) {
                OrderBean cart = new OrderBean();
                cart.setOrder_id(cursor.getString(0));
                Log.e("TAG", "1queryData: "+cursor.getString(0) );
                Log.e("TAG", "2queryData: "+cursor.getString(1) );
                Log.e("TAG", "3queryData: "+cursor.getString(2) );
                Log.e("TAG", "4queryData: "+cursor.getString(3) );
                Log.e("TAG", "5queryData: "+cursor.getString(4) );
                Log.e("TAG", "6queryData: "+cursor.getString(5) );
                cart.setPayStatus(cursor.getString(1));
                cart.setPayMethod(cursor.getString(2));
                cart.setPayBillNumber(cursor.getString(3));
                cart.setPayDate(cursor.getString(4));
                cart.setPayAmount(cursor.getString(5));
                cart.setCashAmount(cursor.getString(6));
                cart.setCashChange(cursor.getString(7));
                cart.setMemberPayToken(cursor.getString(8));
                cart.setSecondPayNumber(cursor.getString(9));
                cart.setMerCode(cursor.getString(10));
                cart.setMerName(cursor.getString(11));
                cart.setTermCode(cursor.getString(12));
                cart.setBankName(cursor.getString(13));
                cart.setBankCard(cursor.getString(14));
                cart.setBankCardExpired(cursor.getString(15));
                cart.setAuthorCode(cursor.getString(16));
                cart.setTradeBatchCode(cursor.getString(17));
                cart.setOrderCode(cursor.getString(18));
                carts.add(cart);
            }
        }
        cursor.close();
        return carts;

    }
}
